<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=11; IE=10; IE=9; IE=8; IE=7; IE=EDGE" />
        <title>自定义操作 | 多线程</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.0.1">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    
    
    <link rel="next" href="./async_pictures.html" />
    
    
    <link rel="prev" href="./nsoperation-03-nsoperation_vs_gcd.html" />
    

        
    </head>
    <body>
        
        
    <link rel="stylesheet" href="gitbook/style.css">
    
    

        
    <div class="book" data-level="0.5.4" data-basepath="." data-revision="Thu Jun 04 2015 06:22:37 GMT+0800 (CST)">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
        
        
        

        

        
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                        <i class="fa fa-check"></i>
                        
                        多线程
                    </a>
                
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="0.1" data-path="multithread.html">
            
                
                    <a href="./multithread.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.1.</b>
                        
                        多线程
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.2" data-path="pthread.html">
            
                
                    <a href="./pthread.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.2.</b>
                        
                        pthread
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.3" data-path="nsthread.html">
            
                
                    <a href="./nsthread.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.3.</b>
                        
                        NSThread
                    </a>
                
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="0.3.1" data-path="nsthread-01-start_thread.html">
            
                
                    <a href="./nsthread-01-start_thread.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.3.1.</b>
                        
                        创建线程的方式
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.3.2" data-path="nsthread-02-target.html">
            
                
                    <a href="./nsthread-02-target.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.3.2.</b>
                        
                        NSThread 的 Target
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.3.3" data-path="nsthread-03-thread_status.html">
            
                
                    <a href="./nsthread-03-thread_status.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.3.3.</b>
                        
                        线程状态
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.3.4" data-path="nsthread-04-properties.html">
            
                
                    <a href="./nsthread-04-properties.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.3.4.</b>
                        
                        线程属性
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.3.5" data-path="nsthread-05-resource.html">
            
                
                    <a href="./nsthread-05-resource.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.3.5.</b>
                        
                        资源共享
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.3.6" data-path="nsthread-06-atomic.html">
            
                
                    <a href="./nsthread-06-atomic.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.3.6.</b>
                        
                        原子属性
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.3.7" data-path="nsthread-07-communication.html">
            
                
                    <a href="./nsthread-07-communication.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.3.7.</b>
                        
                        线程间通讯
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="0.4" data-path="gcd.html">
            
                
                    <a href="./gcd.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.4.</b>
                        
                        GCD
                    </a>
                
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="0.4.1" data-path="gcd-01-sync&amp;async.html">
            
                
                    <a href="./gcd-01-sync&amp;async.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.4.1.</b>
                        
                        同步 &amp; 异步
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.4.2" data-path="gcd-02-block.html">
            
                
                    <a href="./gcd-02-block.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.4.2.</b>
                        
                        block
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.4.3" data-path="gcd-03-common_code.html">
            
                
                    <a href="./gcd-03-common_code.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.4.3.</b>
                        
                        GCD 常用代码
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.4.4" data-path="gcd-04-dispatch_queue_serial.html">
            
                
                    <a href="./gcd-04-dispatch_queue_serial.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.4.4.</b>
                        
                        串行队列
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.4.5" data-path="gcd-05-dispatch_queue_concurrent.html">
            
                
                    <a href="./gcd-05-dispatch_queue_concurrent.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.4.5.</b>
                        
                        并发队列
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.4.6" data-path="gcd-06-main_queue.html">
            
                
                    <a href="./gcd-06-main_queue.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.4.6.</b>
                        
                        主队列
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.4.7" data-path="gcd-07-sync_task.html">
            
                
                    <a href="./gcd-07-sync_task.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.4.7.</b>
                        
                        同步任务的作用
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.4.8" data-path="gcd-08-barrier_async.html">
            
                
                    <a href="./gcd-08-barrier_async.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.4.8.</b>
                        
                        Barrier 异步
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.4.9" data-path="gcd-09-global_queue.html">
            
                
                    <a href="./gcd-09-global_queue.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.4.9.</b>
                        
                        全局队列
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.4.10" data-path="gcd-10-delay.html">
            
                
                    <a href="./gcd-10-delay.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.4.10.</b>
                        
                        延迟操作
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.4.11" data-path="gcd-11-once.html">
            
                
                    <a href="./gcd-11-once.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.4.11.</b>
                        
                        一次性执行
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.4.12" data-path="gcd-12-group.html">
            
                
                    <a href="./gcd-12-group.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.4.12.</b>
                        
                        调度组
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="0.5" data-path="nsoperation.html">
            
                
                    <a href="./nsoperation.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.5.</b>
                        
                        NSOperation
                    </a>
                
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="0.5.1" data-path="nsoperation-01-basic.html">
            
                
                    <a href="./nsoperation-01-basic.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.5.1.</b>
                        
                        基本演练
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.5.2" data-path="nsoperation-02-advance.html">
            
                
                    <a href="./nsoperation-02-advance.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.5.2.</b>
                        
                        高级演练
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.5.3" data-path="nsoperation-03-nsoperation_vs_gcd.html">
            
                
                    <a href="./nsoperation-03-nsoperation_vs_gcd.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.5.3.</b>
                        
                        与 GCD 的对比
                    </a>
                
            
            
        </li>
    
        <li class="chapter active" data-level="0.5.4" data-path="nsoperation-04-custom_operation.html">
            
                
                    <a href="./nsoperation-04-custom_operation.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.5.4.</b>
                        
                        自定义操作
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="0.6" data-path="async_pictures.html">
            
                
                    <a href="./async_pictures.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.6.</b>
                        
                        异步加载图片
                    </a>
                
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="0.6.1" data-path="async_pictures-01-preparation.html">
            
                
                    <a href="./async_pictures-01-preparation.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.6.1.</b>
                        
                        搭建界面&amp;数据准备
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.6.2" data-path="async_pictures-02-sync_picture.html">
            
                
                    <a href="./async_pictures-02-sync_picture.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.6.2.</b>
                        
                        同步加载图像
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.6.3" data-path="async_pictures-03-async_load_pictures.html">
            
                
                    <a href="./async_pictures-03-async_load_pictures.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.6.3.</b>
                        
                        异步加载图像
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.6.4" data-path="async_pictures-04-placeholder_picture.html">
            
                
                    <a href="./async_pictures-04-placeholder_picture.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.6.4.</b>
                        
                        占位图像
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.6.5" data-path="async_pictures-05-mvc.html">
            
                
                    <a href="./async_pictures-05-mvc.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.6.5.</b>
                        
                        MVC
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.6.6" data-path="async_pictures-06-operation_cache.html">
            
                
                    <a href="./async_pictures-06-operation_cache.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.6.6.</b>
                        
                        操作缓冲池
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.6.7" data-path="async_pictures-07-image_cache.html">
            
                
                    <a href="./async_pictures-07-image_cache.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.6.7.</b>
                        
                        图像缓冲池
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.6.8" data-path="async_pictures-08-refactor.html">
            
                
                    <a href="./async_pictures-08-refactor.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.6.8.</b>
                        
                        代码重构
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.6.9" data-path="async_pictures-09-memory_warning.html">
            
                
                    <a href="./async_pictures-09-memory_warning.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.6.9.</b>
                        
                        内存警告
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.6.10" data-path="async_pictures-10-black_list.html">
            
                
                    <a href="./async_pictures-10-black_list.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.6.10.</b>
                        
                        黑名单
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.6.11" data-path="async_pictures-11-sandbox_cache.html">
            
                
                    <a href="./async_pictures-11-sandbox_cache.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.6.11.</b>
                        
                        沙盒缓存
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.6.12" data-path="async_pictures-12-summary.html">
            
                
                    <a href="./async_pictures-12-summary.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.6.12.</b>
                        
                        小结
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.6.13" data-path="async_pictures-13-sdwebimage.html">
            
                
                    <a href="./async_pictures-13-sdwebimage.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.6.13.</b>
                        
                        SDWebImage初体验
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="0.7" data-path="imitate_sdwebimage.html">
            
                
                    <a href="./imitate_sdwebimage.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.7.</b>
                        
                        仿SDWebImage
                    </a>
                
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="0.7.1" data-path="imitate_sdwebimage-01-download_operation.html">
            
                
                    <a href="./imitate_sdwebimage-01-download_operation.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.7.1.</b>
                        
                        下载操作
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.7.2" data-path="imitate_sdwebimage-02-test_download_operation.html">
            
                
                    <a href="./imitate_sdwebimage-02-test_download_operation.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.7.2.</b>
                        
                        测试下载操作
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.7.3" data-path="imitate_sdwebimage-03-download_manager.html">
            
                
                    <a href="./imitate_sdwebimage-03-download_manager.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.7.3.</b>
                        
                        下载管理器
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.7.4" data-path="imitate_sdwebimage-04-custom_imageview.html">
            
                
                    <a href="./imitate_sdwebimage-04-custom_imageview.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.7.4.</b>
                        
                        自定义 UIImageView
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="0.8" data-path="sdwebimage.html">
            
                
                    <a href="./sdwebimage.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.8.</b>
                        
                        SDWebImage
                    </a>
                
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="0.8.1" data-path="sdwebimage-01-framework.html">
            
                
                    <a href="./sdwebimage-01-framework.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.8.1.</b>
                        
                        第三方框架
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.8.2" data-path="sdwebimage-02-mask_enum.html">
            
                
                    <a href="./sdwebimage-02-mask_enum.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.8.2.</b>
                        
                        位移枚举
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.8.3" data-path="sdwebimage-03-nscache.html">
            
                
                    <a href="./sdwebimage-03-nscache.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.8.3.</b>
                        
                        NSCache
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.8.4" data-path="imitate_sdwebimage-05-questions.html">
            
                
                    <a href="./imitate_sdwebimage-05-questions.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.8.4.</b>
                        
                        SDWebImage常见面试题
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="0.9" data-path="runloop.html">
            
                
                    <a href="./runloop.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.9.</b>
                        
                        runloop
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.10" data-path="autoreleasepool.html">
            
                
                    <a href="./autoreleasepool.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.10.</b>
                        
                        自动释放池
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                Published with GitBook
            </a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Table of Contents"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Font Settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Share"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">
                    Share on Twitter
                </button>
                <button type="button" data-sharing="google-plus" class="button">
                    Share on Google
                </button>
                <button type="button" data-sharing="facebook" class="button">
                    Share on Facebook
                </button>
                <button type="button" data-sharing="weibo" class="button">
                    Share on Weibo
                </button>
                <button type="button" data-sharing="instapaper" class="button">
                    Share on Instapaper
                </button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
    
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >多线程</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="%E8%87%AA%E5%AE%9A%E4%B9%89%E6%93%8D%E4%BD%9C">&#x81EA;&#x5B9A;&#x4E49;&#x64CD;&#x4F5C;</h1>
<h2 id="%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C">&#x51C6;&#x5907;&#x5DE5;&#x4F5C;</h2>
<ul>
<li>&#x81EA;&#x5B9A;&#x4E49; <code>DownloadImageOperation</code> &#x7EE7;&#x627F;&#x81EA; NSOperation</li>
<li>&#x4EE3;&#x7801;&#x8C03;&#x7528;</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-comment">// &#x5B9E;&#x4F8B;&#x5316;&#x81EA;&#x5B9A;&#x4E49;&#x64CD;&#x4F5C;</span>
DownloadImageOperation *op = [[DownloadImageOperation alloc] init];
<span class="hljs-comment">// &#x5C06;&#x81EA;&#x5B9A;&#x4E49;&#x64CD;&#x4F5C;&#x6DFB;&#x52A0;&#x5230;&#x4E0B;&#x8F7D;&#x961F;&#x5217;</span>
[<span class="hljs-keyword">self</span><span class="hljs-variable">.downloadQueue</span> addOperation:op];
</code></pre>
<h2 id="%E9%9C%80%E6%B1%82%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91">&#x9700;&#x6C42;&#x9A71;&#x52A8;&#x5F00;&#x53D1;</h2>
<h3 id="%E7%9B%AE%E6%A0%87%E4%B8%80%EF%BC%9A%E8%AE%BE%E7%BD%AE%E8%87%AA%E5%AE%9A%E4%B9%89%E6%93%8D%E4%BD%9C%E7%9A%84%E6%89%A7%E8%A1%8C%E5%85%A5%E5%8F%A3">&#x76EE;&#x6807;&#x4E00;&#xFF1A;&#x8BBE;&#x7F6E;&#x81EA;&#x5B9A;&#x4E49;&#x64CD;&#x4F5C;&#x7684;&#x6267;&#x884C;&#x5165;&#x53E3;</h3>
<blockquote>
<p>&#x5BF9;&#x4E8E;&#x81EA;&#x5B9A;&#x4E49;&#x64CD;&#x4F5C;&#xFF0C;&#x53EA;&#x8981;&#x91CD;&#x5199;&#x4E86; <code>main</code> &#x65B9;&#x6CD5;&#xFF0C;&#x5F53;&#x961F;&#x5217;&#x8C03;&#x5EA6;&#x64CD;&#x4F5C;&#x6267;&#x884C;&#x65F6;&#xFF0C;&#x4F1A;&#x81EA;&#x52A8;&#x8FD0;&#x884C; <code>main</code> &#x65B9;&#x6CD5;</p>
</blockquote>
<p><strong>&#x6CE8;&#x610F;</strong>&#xFF1A;<code>main</code> &#x65B9;&#x6CD5;&#x4E2D;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x81EA;&#x52A8;&#x91CA;&#x653E;&#x6C60;&#xFF01;</p>
<pre><code class="lang-objc">- (<span class="hljs-keyword">void</span>)main {
    <span class="hljs-keyword">@autoreleasepool</span> {
        <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@&quot;%@&quot;</span>, [<span class="hljs-built_in">NSThread</span> currentThread]);
    }
}
</code></pre>
<h3 id="%E7%9B%AE%E6%A0%87%E4%BA%8C%EF%BC%9A%E7%BB%99%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%82%E6%95%B0%E4%BC%A0%E9%80%92%E5%8F%82%E6%95%B0">&#x76EE;&#x6807;&#x4E8C;&#xFF1A;&#x7ED9;&#x81EA;&#x5B9A;&#x4E49;&#x53C2;&#x6570;&#x4F20;&#x9012;&#x53C2;&#x6570;</h3>
<ul>
<li>&#x5B9A;&#x4E49;&#x5C5E;&#x6027;</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-comment">/// &#x8981;&#x4E0B;&#x8F7D;&#x56FE;&#x50CF;&#x7684; URL &#x5B57;&#x7B26;&#x4E32;</span>
<span class="hljs-keyword">@property</span> (<span class="hljs-keyword">nonatomic</span>, <span class="hljs-keyword">copy</span>) <span class="hljs-built_in">NSString</span> *URLString;
</code></pre>
<ul>
<li>&#x4EE3;&#x7801;&#x8C03;&#x7528;</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-comment">// &#x5B9E;&#x4F8B;&#x5316;&#x81EA;&#x5B9A;&#x4E49;&#x64CD;&#x4F5C;</span>
DownloadImageOperation *op = [[DownloadImageOperation alloc] init];
<span class="hljs-comment">// &#x8BBE;&#x7F6E;&#x64CD;&#x4F5C;&#x5C5E;&#x6027;</span>
op<span class="hljs-variable">.URLString</span> = <span class="hljs-string">@&quot;https://www.baidu.com/img/bdlogo.png&quot;</span>;

<span class="hljs-comment">// &#x5C06;&#x81EA;&#x5B9A;&#x4E49;&#x64CD;&#x4F5C;&#x6DFB;&#x52A0;&#x5230;&#x4E0B;&#x8F7D;&#x961F;&#x5217;&#xFF0C;&#x64CD;&#x4F5C;&#x542F;&#x52A8;&#x540E;&#x4F1A;&#x6267;&#x884C; main &#x65B9;&#x6CD5;</span>
[<span class="hljs-keyword">self</span><span class="hljs-variable">.downloadQueue</span> addOperation:op];
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF0C;<code>main</code> &#x65B9;&#x6CD5;&#x88AB;&#x8C03;&#x7528;&#x65F6;&#xFF0C;&#x5C5E;&#x6027;&#x5DF2;&#x7ECF;&#x51C6;&#x5907;&#x5C31;&#x7EEA;</p>
</blockquote>
<h3 id="%E7%9B%AE%E6%A0%87%E4%B8%89%EF%BC%9A%E5%A6%82%E4%BD%95%E5%9B%9E%E8%B0%83%EF%BC%9F">&#x76EE;&#x6807;&#x4E09;&#xFF1A;&#x5982;&#x4F55;&#x56DE;&#x8C03;&#xFF1F;</h3>
<h4 id="%E5%88%A9%E7%94%A8%E7%B3%BB%E7%BB%9F%E6%8F%90%E4%BE%9B%E7%9A%84-completionblock-%E5%B1%9E%E6%80%A7">&#x5229;&#x7528;&#x7CFB;&#x7EDF;&#x63D0;&#x4F9B;&#x7684; <code>CompletionBlock</code> &#x5C5E;&#x6027;</h4>
<pre><code class="lang-objc"><span class="hljs-comment">// &#x8BBE;&#x7F6E;&#x5B8C;&#x6210;&#x56DE;&#x8C03;</span>
[op setCompletionBlock:^{
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@&quot;&#x5B8C;&#x6210; %@&quot;</span>, [<span class="hljs-built_in">NSThread</span> currentThread]);
}];
</code></pre>
<ul>
<li>&#x53EA;&#x8981;&#x8BBE;&#x7F6E;&#x4E86; <code>CompletionBlock</code>&#xFF0C;&#x5F53;&#x64CD;&#x4F5C;&#x6267;&#x884C;&#x5B8C;&#x6BD5;&#x540E;&#xFF0C;&#x5C31;&#x4F1A;&#x88AB;&#x81EA;&#x52A8;&#x8C03;&#x7528;</li>
<li><code>CompletionBlock</code> &#x65E2;&#x4E0D;&#x5728;&#x4E3B;&#x7EBF;&#x7A0B;&#x4E5F;&#x4E0D;&#x5728;&#x64CD;&#x4F5C;&#x6267;&#x884C;&#x6240;&#x5728;&#x7EBF;&#x7A0B;</li>
<li><code>CompletionBlock</code> &#x65E0;&#x6CD5;&#x4F20;&#x9012;&#x53C2;&#x6570;</li>
</ul>
<h4 id="%E8%87%AA%E5%B7%B1%E5%AE%9A%E4%B9%89%E5%9B%9E%E8%B0%83-block%EF%BC%8C%E5%9C%A8%E6%93%8D%E4%BD%9C%E7%BB%93%E6%9D%9F%E5%90%8E%E6%89%A7%E8%A1%8C">&#x81EA;&#x5DF1;&#x5B9A;&#x4E49;&#x56DE;&#x8C03; Block&#xFF0C;&#x5728;&#x64CD;&#x4F5C;&#x7ED3;&#x675F;&#x540E;&#x6267;&#x884C;</h4>
<ul>
<li>&#x5B9A;&#x4E49;&#x5C5E;&#x6027;</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-comment">/// &#x5B8C;&#x6210;&#x56DE;&#x8C03; Block</span>
<span class="hljs-keyword">@property</span> (<span class="hljs-keyword">nonatomic</span>, <span class="hljs-keyword">copy</span>) <span class="hljs-keyword">void</span> (^finishedBlock)(<span class="hljs-built_in">UIImage</span> *image);
</code></pre>
<ul>
<li>&#x8BBE;&#x7F6E;&#x81EA;&#x5B9A;&#x4E49;&#x56DE;&#x8C03;</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-comment">// &#x8BBE;&#x7F6E;&#x81EA;&#x5B9A;&#x4E49;&#x5B8C;&#x6210;&#x56DE;&#x8C03;</span>
[op setFinishedBlock:^(<span class="hljs-built_in">UIImage</span> *image) {
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@&quot;finished %@ %@&quot;</span>, [<span class="hljs-built_in">NSThread</span> currentThread], image);
}];
</code></pre>
<ul>
<li>&#x8017;&#x65F6;&#x64CD;&#x4F5C;&#x540E;&#x6267;&#x884C;&#x56DE;&#x8C03;</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-comment">// &#x5224;&#x65AD;&#x81EA;&#x5B9A;&#x4E49;&#x56DE;&#x8C03;&#x662F;&#x5426;&#x5B58;&#x5728;</span>
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">self</span><span class="hljs-variable">.finishedBlock</span> != <span class="hljs-literal">nil</span>) {
    <span class="hljs-comment">// &#x901A;&#x5E38;&#x4E3A;&#x4E86;&#x7B80;&#x5316;&#x8C03;&#x7528;&#x65B9;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#x7ED3;&#x675F;&#x540E;&#x7684;&#x56DE;&#x8C03;&#xFF0C;&#x5927;&#x591A;&#x5728;&#x4E3B;&#x7EBF;&#x7A0B;</span>
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        <span class="hljs-keyword">self</span><span class="hljs-variable">.finishedBlock</span>(<span class="hljs-string">@&quot;hello&quot;</span>);
    }];
}
</code></pre>
<h3 id="%E7%9B%AE%E6%A0%87%E5%9B%9B%EF%BC%9A%E7%AE%80%E5%8C%96%E6%93%8D%E4%BD%9C%E5%88%9B%E5%BB%BA">&#x76EE;&#x6807;&#x56DB;&#xFF1A;&#x7B80;&#x5316;&#x64CD;&#x4F5C;&#x521B;&#x5EFA;</h3>
<ul>
<li>&#x5B9A;&#x4E49;<code>&#x7C7B;</code>&#x65B9;&#x6CD5;</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-comment">///  &#x5B9E;&#x4F8B;&#x5316;&#x4E0B;&#x8F7D;&#x56FE;&#x50CF;&#x64CD;&#x4F5C;</span>
<span class="hljs-comment">///</span>
<span class="hljs-comment">///  @param URLString &#x56FE;&#x50CF; URL &#x5B57;&#x7B26;&#x4E32;</span>
<span class="hljs-comment">///  @param finished  &#x5B8C;&#x6210;&#x56DE;&#x8C03; Block</span>
<span class="hljs-comment">///</span>
<span class="hljs-comment">///  @return &#x4E0B;&#x8F7D;&#x64CD;&#x4F5C;&#x5B9E;&#x4F8B;</span>
+ (instancetype)downloadImageOperationWithURLString:(<span class="hljs-built_in">NSString</span> *)URLString finished:(<span class="hljs-keyword">void</span> (^)(<span class="hljs-built_in">UIImage</span> *image))finished;
</code></pre>
<ul>
<li>&#x5B9E;&#x73B0;&#x65B9;&#x6CD5;</li>
</ul>
<pre><code class="lang-objc">+ (instancetype)downloadImageOperationWithURLString:(<span class="hljs-built_in">NSString</span> *)URLString finished:(<span class="hljs-keyword">void</span> (^)(<span class="hljs-built_in">UIImage</span> *))finished {
    DownloadImageOperation *op = [[DownloadImageOperation alloc] init];

    op<span class="hljs-variable">.URLString</span> = URLString;
    op<span class="hljs-variable">.finishedBlock</span> = finished;

    <span class="hljs-keyword">return</span> op;
}
</code></pre>
<ul>
<li>&#x65B9;&#x6CD5;&#x8C03;&#x7528;</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-comment">// &#x4F7F;&#x7528;&#x7C7B;&#x65B9;&#x6CD5;&#x5B9E;&#x4F8B;&#x5316;&#x4E0B;&#x8F7D;&#x64CD;&#x4F5C;</span>
DownloadImageOperation *op = [DownloadImageOperation downloadImageOperationWithURLString:<span class="hljs-string">@&quot;http://www.baidu.com/img/bdlogo.png&quot;</span> finished:^(<span class="hljs-built_in">UIImage</span> *image) {
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@&quot;%@&quot;</span>, image);
}];

<span class="hljs-comment">// &#x5C06;&#x81EA;&#x5B9A;&#x4E49;&#x64CD;&#x4F5C;&#x6DFB;&#x52A0;&#x5230;&#x4E0B;&#x8F7D;&#x961F;&#x5217;&#xFF0C;&#x64CD;&#x4F5C;&#x542F;&#x52A8;&#x540E;&#x4F1A;&#x6267;&#x884C; main &#x65B9;&#x6CD5;</span>
[<span class="hljs-keyword">self</span><span class="hljs-variable">.downloadQueue</span> addOperation:op];
</code></pre>
<h3 id="%E7%9B%AE%E6%A0%87%E4%BA%94%EF%BC%9A%E5%8F%96%E6%B6%88%E6%93%8D%E4%BD%9C">&#x76EE;&#x6807;&#x4E94;&#xFF1A;&#x53D6;&#x6D88;&#x64CD;&#x4F5C;</h3>
<blockquote>
<p>&#x5728;&#x5173;&#x952E;&#x8282;&#x70B9;&#x6DFB;&#x52A0; <code>isCancelled</code> &#x5224;&#x65AD;</p>
</blockquote>
<ul>
<li>&#x6DFB;&#x52A0;&#x591A;&#x4E2A;&#x4E0B;&#x8F7D;&#x64CD;&#x4F5C;</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; ++i) {
    <span class="hljs-built_in">NSString</span> *urlString = [<span class="hljs-built_in">NSString</span> stringWithFormat:<span class="hljs-string">@&quot;http://www.xxx.com/%04d.png&quot;</span>, i];

    DownloadImageOperation *op = [DownloadImageOperation downloadImageOperationWithURLString:urlString finished:^(<span class="hljs-built_in">UIImage</span> *image) {
        <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@&quot;===&gt; %@&quot;</span>, image);
    }];

    <span class="hljs-comment">// &#x5C06;&#x81EA;&#x5B9A;&#x4E49;&#x64CD;&#x4F5C;&#x6DFB;&#x52A0;&#x5230;&#x4E0B;&#x8F7D;&#x961F;&#x5217;&#xFF0C;&#x64CD;&#x4F5C;&#x542F;&#x52A8;&#x540E;&#x4F1A;&#x6267;&#x884C; main &#x65B9;&#x6CD5;</span>
    [<span class="hljs-keyword">self</span><span class="hljs-variable">.downloadQueue</span> addOperation:op];
}
</code></pre>
<ul>
<li>&#x8BBE;&#x7F6E;&#x961F;&#x5217;&#x6700;&#x5927;&#x5E76;&#x53D1;&#x64CD;&#x4F5C;&#x6570;</li>
</ul>
<pre><code class="lang-objc">_downloadQueue<span class="hljs-variable">.maxConcurrentOperationCount</span> = <span class="hljs-number">2</span>;
</code></pre>
<ul>
<li>&#x5185;&#x5B58;&#x8B66;&#x544A;&#x65F6;&#x53D6;&#x6D88;&#x6240;&#x6709;&#x64CD;&#x4F5C;</li>
</ul>
<pre><code class="lang-objc">- (<span class="hljs-keyword">void</span>)didReceiveMemoryWarning {
    [<span class="hljs-keyword">super</span> didReceiveMemoryWarning];

    [<span class="hljs-keyword">self</span><span class="hljs-variable">.downloadQueue</span> cancelAllOperations];
}
</code></pre>
<blockquote>
<p><code>cancelAllOperations</code> &#x4F1A;&#x5411;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x64CD;&#x4F5C;&#x53D1;&#x9001; <code>Cancel</code> &#x6D88;&#x606F;</p>
</blockquote>
<ul>
<li>&#x8C03;&#x6574; <code>main</code> &#x65B9;&#x6CD5;&#xFF0C;&#x5728;&#x5173;&#x952E;&#x8282;&#x70B9;&#x5224;&#x65AD;</li>
</ul>
<pre><code class="lang-objc">- (<span class="hljs-keyword">void</span>)main {
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@&quot;%s&quot;</span>, __FUNCTION__);

    <span class="hljs-keyword">@autoreleasepool</span> {

        <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@&quot;&#x4E0B;&#x8F7D;&#x56FE;&#x50CF; %@&quot;</span>, <span class="hljs-keyword">self</span><span class="hljs-variable">.URLString</span>);
        <span class="hljs-comment">// &#x6A21;&#x62DF;&#x5EF6;&#x65F6;</span>
        [<span class="hljs-built_in">NSThread</span> sleepForTimeInterval:<span class="hljs-number">1.0</span>];

        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">self</span><span class="hljs-variable">.isCancelled</span>) {
            <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@&quot;1.--- &#x8FD4;&#x56DE;&quot;</span>);
            <span class="hljs-keyword">return</span>;
        }

        <span class="hljs-comment">// &#x5224;&#x65AD;&#x81EA;&#x5B9A;&#x4E49;&#x56DE;&#x8C03;&#x662F;&#x5426;&#x5B58;&#x5728;</span>
        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">self</span><span class="hljs-variable">.finishedBlock</span> != <span class="hljs-literal">nil</span>) {
            <span class="hljs-comment">// &#x901A;&#x5E38;&#x4E3A;&#x4E86;&#x7B80;&#x5316;&#x8C03;&#x7528;&#x65B9;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#x7ED3;&#x675F;&#x540E;&#x7684;&#x56DE;&#x8C03;&#xFF0C;&#x5927;&#x591A;&#x5728;&#x4E3B;&#x7EBF;&#x7A0B;</span>
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                <span class="hljs-keyword">self</span><span class="hljs-variable">.finishedBlock</span>(<span class="hljs-keyword">self</span><span class="hljs-variable">.URLString</span>);
            }];
        }
    }
}

- (<span class="hljs-keyword">void</span>)start {
    [<span class="hljs-keyword">super</span> start];

    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@&quot;%s&quot;</span>, __FUNCTION__);
}
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x5982;&#x679C;&#x64CD;&#x4F5C;&#x72B6;&#x6001;&#x5DF2;&#x7ECF;&#x662F; <code>Cancel</code>&#xFF0C;&#x5219;&#x4E0D;&#x4F1A;&#x6267;&#x884C; <code>main</code> &#x51FD;&#x6570;</p>
</blockquote>
<ul>
<li>&#x961F;&#x5217;&#x8C03;&#x5EA6;&#x64CD;&#x4F5C;&#x65F6;&#xFF0C;&#x9996;&#x5148;&#x6267;&#x884C; <code>start</code> &#x65B9;&#x6CD5;&#x5C06;&#x7EBF;&#x7A0B;&#x653E;&#x5165;<code>&#x53EF;&#x8C03;&#x5EA6;&#x7EBF;&#x7A0B;&#x6C60;</code></li>
<li>&#x64CD;&#x4F5C;&#x6267;&#x884C;&#x65F6;&#x7684;&#x5165;&#x53E3;&#x662F; <code>main</code> &#x65B9;&#x6CD5;</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./nsoperation-03-nsoperation_vs_gcd.html" class="navigation navigation-prev " aria-label="Previous page: 与 GCD 的对比"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./async_pictures.html" class="navigation navigation-next " aria-label="Next page: 异步加载图片"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
